#!/bin/sh

cleanup () {
  case "${TEST_DIR-}" in /*) rm -rf "${TEST_DIR}" ;; esac
  unset -f nvm_grep
}
die () { echo "$@" ; cleanup ; exit 1; }

: nvm.sh
\. ../../../nvm.sh

\. ../../common.sh

TEST_DIR="$(mktemp -d)" || die 'unable to create a temp dir'
NPMRC="${TEST_DIR}/test.npmrc"

assert_bad_news_file () {
  try nvm_npmrc_bad_news_bears "$1"
  [ "_${CAPTURED_EXIT_CODE}" = "_0" ] || die "$2: exit code >${CAPTURED_EXIT_CODE}<, expected >0<"
  try_err nvm_npmrc_bad_news_bears "$1"
  [ "_${CAPTURED_STDOUT}" = "_" ] || die "$2: stdout >${CAPTURED_STDOUT}<, expected ><"
  [ "_${CAPTURED_STDERR}" = "_" ] || die "$2: stderr >${CAPTURED_STDERR}<, expected ><"
}

assert_bad_news () {
  printf '%b' "$1" > "${NPMRC}"
  assert_bad_news_file "${NPMRC}" "$2"
}

assert_good_news () {
  try nvm_npmrc_bad_news_bears "$@"
  [ "_${CAPTURED_EXIT_CODE}" = "_1" ] || die "${GOOD_NEWS_LABEL}: exit code >${CAPTURED_EXIT_CODE}<, expected >1<"
  try_err nvm_npmrc_bad_news_bears "$@"
  [ "_${CAPTURED_STDOUT}" = "_" ] || die "${GOOD_NEWS_LABEL}: stdout >${CAPTURED_STDOUT}<, expected ><"
  [ "_${CAPTURED_STDERR}" = "_" ] || die "${GOOD_NEWS_LABEL}: stderr >${CAPTURED_STDERR}<, expected ><"
}

assert_good_news_content () {
  printf '%b' "$1" > "${NPMRC}"
  GOOD_NEWS_LABEL="$2"
  assert_good_news "${NPMRC}"
}

assert_bad_news 'prefix=x\n' 'prefix=x'
assert_bad_news 'globalconfig=x\n' 'globalconfig=x'
assert_bad_news 'prefix   =  /some/path\n' 'spaces before ='
assert_bad_news 'globalconfig = x\n' 'globalconfig = x'
assert_bad_news 'prefix=\n' 'an empty value'
assert_bad_news 'prefix==x\n' 'a value starting with ='
assert_bad_news 'prefix=x\r\n' 'a CRLF line ending after the value'
assert_bad_news 'prefix=x' 'no trailing newline'
assert_bad_news 'a=1\n\n; comment\nprefix=x\nb=2\n' 'a match among other lines'
assert_bad_news 'a=1\nglobalconfig=x' 'a final match without a trailing newline'
assert_bad_news 'prefix\nglobalconfig=x\n' 'a bare key line followed by a match'
assert_bad_news 'prefix=a=b c\n' 'a value with = and spaces'

: > "${NPMRC}"
I=0
while [ "${I}" -lt 50 ]; do
  printf 'k%s=v\n' "${I}" >> "${NPMRC}"
  I=$((I + 1))
done
unset I
printf 'prefix=x\n' >> "${NPMRC}"
assert_bad_news_file "${NPMRC}" 'a match after 50 unrelated lines'

assert_good_news_content '' 'an empty file'
assert_good_news_content '\n\n' 'only blank lines'
assert_good_news_content 'a=1\nb=2' 'unrelated keys'
assert_good_news_content 'prefixfoo=x\n' 'a key starting with prefix'
assert_good_news_content 'fooprefix=x\n' 'a key ending with prefix'
assert_good_news_content 'globalconfigs=x\n' 'a key starting with globalconfig'
assert_good_news_content 'xglobalconfig=x\n' 'a key ending with globalconfig'
assert_good_news_content 'foo=prefix=x\n' 'prefix after the first ='
assert_good_news_content 'foo=globalconfig\n' 'globalconfig as a value'
assert_good_news_content 'prefix x=x\n' 'a space inside the key'
assert_good_news_content 'pre\\fix=x\nglobal\\config=x\n' 'a backslash inside the key'
assert_good_news_content 'Prefix=x\nGLOBALCONFIG=x\n' 'uppercase keys'
assert_good_news_content ';prefix=x\n#globalconfig=x\n' 'commented-out keys'
assert_good_news_content '=x\n   =x\n' 'empty keys'
assert_good_news_content 'userconfig=x\ncache=x\n' 'other npm config keys'

GOOD_NEWS_LABEL='no argument'
assert_good_news
GOOD_NEWS_LABEL='an empty argument'
assert_good_news ''
GOOD_NEWS_LABEL='a missing file'
assert_good_news "${TEST_DIR}/nonexistent.npmrc"

mkdir "${TEST_DIR}/dir.npmrc" || die 'unable to create a directory'
GOOD_NEWS_LABEL='a directory'
assert_good_news "${TEST_DIR}/dir.npmrc"

printf 'prefix=x\n' > "${TEST_DIR}/real.npmrc"
ln -s "${TEST_DIR}/real.npmrc" "${TEST_DIR}/link.npmrc" || die 'unable to create a symlink'
assert_bad_news_file "${TEST_DIR}/link.npmrc" 'a symlink to a file with prefix'

ln -s "${TEST_DIR}/nonexistent.npmrc" "${TEST_DIR}/dangling.npmrc" || die 'unable to create a dangling symlink'
GOOD_NEWS_LABEL='a dangling symlink'
assert_good_news "${TEST_DIR}/dangling.npmrc"

UNREADABLE="${TEST_DIR}/unreadable.npmrc"
printf 'prefix=x\n' > "${UNREADABLE}"
chmod 000 "${UNREADABLE}" || die 'unable to make a file unreadable'
if ! ( : < "${UNREADABLE}" ) 2>/dev/null; then
  try nvm_npmrc_bad_news_bears "${UNREADABLE}"
  [ "_${CAPTURED_EXIT_CODE}" = "_1" ] || die "an unreadable file: exit code >${CAPTURED_EXIT_CODE}<, expected >1<"
fi
unset UNREADABLE

SPECIAL="${TEST_DIR}/a dir with spaces \$HOME *"
mkdir "${SPECIAL}" || die 'unable to create a directory with special characters'
printf 'globalconfig=x\n' > "${SPECIAL}/.npmrc"
try_err nvm_npmrc_bad_news_bears "${SPECIAL}/.npmrc"
[ "_${CAPTURED_EXIT_CODE}" = "_0" ] || die "a path with spaces and metacharacters: exit code >${CAPTURED_EXIT_CODE}<, expected >0<"
[ "_${CAPTURED_STDERR}" = "_" ] || die "a path with spaces and metacharacters: stderr >${CAPTURED_STDERR}<, expected ><"

printf 'prefix=x\n' > "${NPMRC}"
try_err nvm_npmrc_bad_news_bears "${NPMRC}" "${TEST_DIR}/nonexistent.npmrc"
[ "_${CAPTURED_EXIT_CODE}" = "_0" ] || die "extra arguments: exit code >${CAPTURED_EXIT_CODE}<, expected >0<"
[ "_${CAPTURED_STDERR}" = "_" ] || die "extra arguments: stderr >${CAPTURED_STDERR}<, expected ><"
try_err nvm_npmrc_bad_news_bears "${TEST_DIR}/nonexistent.npmrc" "${NPMRC}"
[ "_${CAPTURED_EXIT_CODE}" = "_1" ] || die "only the first argument is read: exit code >${CAPTURED_EXIT_CODE}<, expected >1<"
[ "_${CAPTURED_STDERR}" = "_" ] || die "only the first argument is read: stderr >${CAPTURED_STDERR}<, expected ><"

NVM_NPMRC=outer-npmrc
NVM_NPMRC_LINE=outer-line
NVM_NPMRC_KEY=outer-key
OLD_IFS="${IFS}"
printf 'a=1\nprefix=x\n' > "${NPMRC}"
nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a direct call did not detect prefix'
printf 'a=1\nb=2\n' > "${NPMRC}"
! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'a direct call detected prefix that is not there'
[ "_${NVM_NPMRC}" = "_outer-npmrc" ] || die "NVM_NPMRC leaked: got >${NVM_NPMRC}<, expected >outer-npmrc<"
[ "_${NVM_NPMRC_LINE}" = "_outer-line" ] || die "NVM_NPMRC_LINE leaked: got >${NVM_NPMRC_LINE}<, expected >outer-line<"
[ "_${NVM_NPMRC_KEY}" = "_outer-key" ] || die "NVM_NPMRC_KEY leaked: got >${NVM_NPMRC_KEY}<, expected >outer-key<"
[ "_${IFS}" = "_${OLD_IFS}" ] || die 'IFS was changed'
unset NVM_NPMRC NVM_NPMRC_LINE NVM_NPMRC_KEY OLD_IFS

printf 'prefix=x\n' > "${NPMRC}"
STDIN_LINE="$(printf 'from-stdin\n' | {
  nvm_npmrc_bad_news_bears "${NPMRC}" >/dev/null 2>&1
  IFS= read -r L
  printf '%s' "${L}"
})"
[ "_${STDIN_LINE}" = "_from-stdin" ] || die "stdin was consumed: got >${STDIN_LINE}<, expected >from-stdin<"

nvm_grep () { : > "${TEST_DIR}/grep-was-called"; return 1; }
printf 'prefix=x\n' > "${NPMRC}"
nvm_npmrc_bad_news_bears "${NPMRC}" || die 'with nvm_grep mocked, prefix was not detected'
[ ! -e "${TEST_DIR}/grep-was-called" ] || die 'nvm_grep was called for a matching file'
printf 'a=1\n' > "${NPMRC}"
! nvm_npmrc_bad_news_bears "${NPMRC}" || die 'with nvm_grep mocked, prefix was detected in a file without it'
[ ! -e "${TEST_DIR}/grep-was-called" ] || die 'nvm_grep was called for a non-matching file'

printf 'a=1\nprefix=x\n' > "${NPMRC}"
(PATH=/nonexistent; nvm_npmrc_bad_news_bears "${NPMRC}") || die 'prefix was not detected without external commands'
printf 'a=1\n' > "${NPMRC}"
(PATH=/nonexistent; ! nvm_npmrc_bad_news_bears "${NPMRC}") || die 'prefix was detected without external commands in a file without it'

cleanup
